package com.example.yiwei.crm.view.activity.JYCoordingActivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.view.activity.coordinationActivity.SeacherJLActivity;
import com.example.yiwei.crm.view.activity.coordinationActivity.SimpleJLActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class JYSearchJLActivity extends AppCompatActivity {
    private TimePickerView mpv1;
    private TimePickerView mpv2;
    String stime;
    String etime;
    TextView stimetv,etimetv;
    private ImageView img_back;
    Date date1,date2;
    int id;
    ImageView img_tj,img_find,img_lr,img_me;
    private LinearLayout linearLayout;
    private ImageView img_qk,img_qk1;
    private Button btn_commit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jysearch_jl);
        btn_commit = (Button) findViewById(R.id.btn_commit);
        img_qk= (ImageView) findViewById(R.id.qingkong);
        img_qk1= (ImageView) findViewById(R.id.qingkong1);
        stimetv = (TextView) findViewById(R.id.start_time);
        etimetv = (TextView) findViewById(R.id.end_time);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        img_lr= (ImageView) findViewById(R.id.item_image_lr);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        img_back= (ImageView) findViewById(R.id.detailed_doc_img_back);
        img_find= (ImageView) findViewById(R.id.item_image_cx);
        linearLayout= (LinearLayout) findViewById(R.id.search_linearLayout);
        id = getIntent().getIntExtra("id",0);
        //创建选择器
        mpv1 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv1.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv1.setTime(new Date());
        //选择监听
        mpv1.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date11) {
                date1 =date11;
                stime=getTime(date11);
                stimetv.setText(stime);
            }
        });
        mpv2 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar1 = Calendar.getInstance();
        mpv2.setRange(calendar1.get(Calendar.YEAR) - 100, calendar1.get(Calendar.YEAR) + 100);
        //插入时间
        mpv2.setTime(new Date());
        //选择监听
        mpv2.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date12) {
                date2 = date12;
                etime=getTime(date12);
                etimetv.setText(etime);
            }
        });
        img_qk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stimetv.setText("");
            }
        });
        img_qk1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etimetv.setText("");
            }
        });
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
            img_lr.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(JYSearchJLActivity.this, JDcoorActivity.class);
                    startActivity(intent);
                    finish();

                }
            });
            img_tj.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(JYSearchJLActivity.this, JDcoorActivity.class);
                    intent.putExtra("tt","tt");
                    startActivity(intent);
                    finish();
                }
            });
            img_find.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(JYSearchJLActivity.this, JDcoorActivity.class);
                    intent.putExtra("ii","ii");
                    startActivity(intent);
                    finish();
                }
            });
            img_me.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(JYSearchJLActivity.this, JDcoorActivity.class);
                    intent.putExtra("me","me");
                    startActivity(intent);
                    finish();
                }
            });


        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("k3".equals(getIntent().getStringExtra("tag3"))){
                    if (date1!=null&&date2!=null&&date1.after(date2)){
                        Toast.makeText(JYSearchJLActivity.this, "开始时间不能在结束时间之后", Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(JYSearchJLActivity.this, JYCooredJLActivity.class);
                        if (!stimetv.getText().toString().equals("")) {
                            intent.putExtra("KJA02_B", stimetv.getText().toString());
                        }
                        if (!etimetv.getText().toString().equals("")) {
                            intent.putExtra("KJA02_E", etimetv.getText().toString());
                        }
                        intent.putExtra("dockid", getIntent().getIntExtra("dockid", 0));
                        startActivity(intent);
                        finish();
                    }
                }else{
                    if (date1!=null&&date2!=null&&!date1.after(date2)){
                        Toast.makeText(JYSearchJLActivity.this, "开始时间不能在结束时间之后", Toast.LENGTH_SHORT).show();

                    }else {
                        Intent intent = new Intent(JYSearchJLActivity.this, SimpleJLActivity.class);
                        if (!stimetv.getText().toString().equals("")) {
                            intent.putExtra("KJA02_B", stimetv.getText().toString());
                        }
                        if (!etimetv.getText().toString().equals("")) {
                            intent.putExtra("KJA02_E", etimetv.getText().toString());
                        }
                        intent.putExtra("dockid", getIntent().getIntExtra("dockid", 0));
                        SimpleJLActivity.b.finish();
                        startActivity(intent);
                        finish();
                    }
                }


            }
        });
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_show:
                mpv1.show();
                break;
            case R.id.end_show:
                mpv2.show();
                break;
        }
    }

}
